
# 网页需要自己唤起打印
def printable_url(url: str):
    print('准备打印...')
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    import time
    import json

    options = Options()
    options.binary_location = './chrome/chrome.exe'
    # printer = 'Microsoft Print to PDF'
    # printer = 'Save as PDF'
    printer = "Jolimark 24-pin printer"
    appState = {
        "recentDestinations": [{"id": printer, "origin": "local"}],
        "selectedDestinationId": printer,
        "version": 2,
        "mediaSize": {"height_microns": 355600, "width_microns": 215900},
        "isHeaderFooterEnabled": False,
    }

    profile = {
        "printing.print_preview_sticky_settings.appState": json.dumps(appState),
        "savefile.default_directory": "D:\\code\\print-url\\target",
    }
    options.add_experimental_option("prefs", profile)
    options.add_argument('--kiosk-printing')
    options.add_argument('--disable-gpu')

    service = Service(executable_path="./chromedriver.exe")

    driver = webdriver.Chrome(service=service, options=options)
    driver.get(url)
    time.sleep(5)
    driver.quit()

from http.server import HTTPServer, BaseHTTPRequestHandler
import json
class TestHTTPHandle(BaseHTTPRequestHandler):
    def do_POST(self):
        print('收到请求!')
        target = self.path[1:]

        self.send_response(200)
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header("Content-Type", "text/plain")
        self.end_headers()
        self.wfile.write('ok'.encode())
        
        if target.startswith('http'):
            printable_url(target)
def start_server():
    # 0.0.0.0 映射测试机器的公网地址、在回掉地址测和订阅测，配置对应机器的公务地址
    http_server = HTTPServer(('10.0.0.4', int(8080)), TestHTTPHandle)
    print('开始监听...')
    http_server.serve_forever()
if __name__ == '__main__':
    print('打印转述服务启动...')
    start_server()